<?php

namespace app\common\model\user;


use app\common\enum\user\UserEnum;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use app\common\model\travel\TravelAgency;
use app\common\service\FileService;
use think\model\concern\SoftDelete;

/**
 * 用户模型
 * Class User
 * @package app\common\model\user
 */
class User extends BaseModel
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';


    /**
     * @notes 关联用户授权模型
     * @return \think\model\relation\HasOne
     */
    public function userAuth()
    {
        return $this->hasOne(UserAuth::class, 'user_id');
    }


    /**
     * @notes 搜索器-用户信息
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchKeywordAttr($query, $value, $data)
    {
        if ($value) {
            $query->where('nickname|mobile|account', 'like', '%' . $value . '%');
        }
    }

    /**
     * @notes 头像获取器 - 用于头像地址拼接域名
     * @param $value
     * @return string
     */
    public function getAvatarAttr($value)
    {
        return trim($value) ? FileService::getFileUrl($value) : '';
    }

    /**
     * @notes 获取器-性别描述
     * @param $value
     * @param $data
     * @return string|string[]
     */
    public function getSexValueAttr($value, $data)
    {
        return DictData::where('type_value', 'sex')->where('value',$data['sex'])->value('name');
    }

    /**
     * @notes 获取器-用户类别
     * @param $value
     * @param $data
     * @return string|string[]
     */
    public function getUserTypeValueAttr($value, $data)
    {
        return DictData::where('type_value', 'user_type')->where('value',$data['user_type'])->value('name');
    }

    /**
     * @notes 获取器-旅行社
     * @param $value
     * @param $data
     * @return string|string[]
     */
    public function getTravelAgencyNameAttr($value, $data)
    {
        return TravelAgency::where('id', $data['travel_agency_id'])->value('name');
    }

    /**
     * @notes 获取器-是否禁用
     * @param $value
     * @param $data
     * @return string|string[]
     */
    public function getIsDisableValueAttr($value, $data)
    {
        return DictData::where('type_value', 'system_disable')->where('value',$data['is_disable'])->value('name');
    }

    /**
     * @notes 登录时间
     * @param $value
     * @return string
     */
    public function getLoginTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getDriverAttr($value,$data)
    {
        $driver = DriverCredential::where('user_id',$data['id'])->value('id');
        if($driver){
            return $driver;
        }else{
            return '';
        }
    }

    public function getDriverStatusAttr($value,$data)
    {
        $driver = DriverCredential::where('user_id',$data['id'])->value('status');
        if(isset($driver)){
            return $driver;
        }else{
            return '';
        }
    }

}